/******************************************************************* 
File name	:	file.c
description:	
History	:
2011/07/13   created  by chengxiang 
*******************************************************************/
#include <vxworks.h>
#include <stdio.h>
#include <taskLib.h>
#include "string.h"
#include "ioLib.h"
#include "intLib.h"
#include "dosFsLib.h"
#include "file.h"
#include "bsp.h"


#ifdef BSP_DRIVER_DBG_MODE
#define FILE_DBG_INF_PRINT
#define FILE_DRV_DEBUG
#endif


/*****************************************************************

input:	UINT8* fileName

output:	UINT32* nByte

return:	BSP_OK, BSP_ERR_OPEN_FILE

description:

get file size.

*****************************************************************/
  STATUS bspSizeOfFile (UINT8* fileName, UINT32* nByte)
{
	int 		fd;
	UINT32 	tmp;
	UINT8	ch[1024];
	UINT8*	pData;

	if ((fd = open (fileName, O_RDONLY, 0)) == ERROR)
	{
#ifdef FILE_DBG_INF_PRINT    	
		printf ("Error: can't open file %s\n", fileName);
#endif
    	return (BSP_ERR_OPEN_FILE);
	}

	pData = ch;
	tmp = read (fd, pData, 1024);
	*(UINT32*) nByte = tmp;
	while (tmp >0)
	{
		tmp = read (fd, pData, 1024);
		*(UINT32*) nByte += tmp;
	}

	close (fd);

	return (BSP_OK);

}


/*****************************************************************

input:	UINT8* fileName

output:	UINT32* nByte

return:	BSP_OK, BSP_ERR_OPEN_FILE

description:

get file size.

*****************************************************************/
/*STATUS bspSizeOfFile_fopen (UINT8* fileName, UINT32* nByte)
{
    	FILE*  	fh;

    	if ((fh = fopen (fileName, "rb")) == NULL)
    	{*/
/*#ifdef FILE_DBG_INF_PRINT    */	
/*		printf ("Error: can't open file %s\n", fileName);*/
/*#endif*/	/*
        	return (BSP_ERR_OPEN_FILE);
    	}

	fseek (fh, 0, SEEK_END);
	
      	*(UINT32*) nByte = ftell (fh);

      	fclose (fh);
    	
    	return (BSP_OK);
 
}*/


/*****************************************************************

input:	UINT8* fileName, UINT32 maxLen

output:	UINT8* pData, UINT32* len

return:	BSP_OK, BSP_ERR_OPEN_FILE, BSP_ERR_READ_FILE,
		BSP_ERR_PARAMETER_INVALID

description:

read file.

*****************************************************************/
STATUS bspReadFile (UINT8* fileName, UINT32 maxLen, UINT8* pData, UINT32* len)
{
	int 	fd;

	if (pData == NULL)
		return (BSP_ERR_PARAMETER_INVALID);

	if ((fd = open (fileName, O_RDONLY, 0)) == ERROR)
	{
#ifdef FILE_DBG_INF_PRINT    	
		printf ("Error: can't open file %s\n", fileName);
#endif
		return (BSP_ERR_OPEN_FILE);
	}

	*(UINT32*)len = read (fd, (char *)pData, maxLen);

	close (fd);

	if (*(UINT32*)len <= maxLen)
	{
		return (BSP_OK);
	}
	else
	{
	          
#ifdef FILE_DBG_INF_PRINT   
		printf ("len = %x,maxLen =%x \n", *len,maxLen);
		printf ("Error: read file %s\n", fileName);
#endif
		return (BSP_ERR_READ_FILE);
	}
}


/*****************************************************************

input:	UINT8* fileName, UINT8* pData, UINT32 len

output:	none

return:	BSP_OK, BSP_ERR_OPEN_FILE, BSP_ERR_WRITE_FILE
		BSP_ERR_PARAMETER_INVALID

description:

write file.

*****************************************************************/
STATUS bspWriteFile (UINT8* fileName, UINT8* pData, UINT32 len)
{
	int 	fd;

	if (pData == NULL)
		return (BSP_ERR_PARAMETER_INVALID);

	if ((fd = open (fileName, O_CREAT | O_WRONLY, 0)) == ERROR)
	{
#ifdef FILE_DBG_INF_PRINT     	
		printf ("Error: can't open file %s\n", fileName);
#endif
    	return (BSP_ERR_OPEN_FILE);
	}

	if (len != write (fd, (char *)pData, len))
	{
    	close (fd);
#ifdef FILE_DBG_INF_PRINT 	    	
		printf ("Error: write file %s.\n", fileName);
#endif
		return (BSP_ERR_WRITE_FILE);
	}
	else
	{
    	close (fd);
#ifdef FILE_DBG_INF_PRINT 	    	
    	printf ("saved file %s successfully.\n", fileName);
#endif
    	return (BSP_OK);
	}

}


/*****************************************************************

input:	UINT8* fileName, UINT8* pData, UINT32 len

output:	none

return:	BSP_OK, BSP_ERR_OPEN_FILE, BSP_ERR_WRITE_FILE
		BSP_ERR_PARAMETER_INVALID

description:

create file.

*****************************************************************/

STATUS bspCreateFile (UINT8* fileName, UINT8* pData, UINT32 len)
{
	int 	fd;

	if (pData == NULL)
		return (BSP_ERR_PARAMETER_INVALID);

	if ((fd = creat (fileName, O_WRONLY)) == ERROR)
	{
#ifdef FILE_DBG_INF_PRINT     	
		printf ("Error: can't open file %s\n", fileName);
#endif
    	return (BSP_ERR_OPEN_FILE);
	}

	if (len != write (fd, (char *)pData, len))
	{
    	close (fd);
#ifdef FILE_DBG_INF_PRINT 	    	
		printf ("Error: write file %s.\n", fileName);
#endif
		return (BSP_ERR_WRITE_FILE);
	}
	else
	{
    	close (fd);
#ifdef FILE_DBG_INF_PRINT 	    	
    	printf ("saved file %s successfully.\n", fileName);
#endif
    	return (BSP_OK);
	}

}


/******************************************************************
input:	UINT8* fileSrc, UINT8* fileDes

output:	none

return:	BSP_OK, BSP_ERR_OPEN_FILE, BSP_ERR_MEM_MALLOC,
		BSP_ERR_WRITE_FILE

description: (Test only)

copy file.

******************************************************************/
STATUS bspCopyFile (UINT8* fileSrc, UINT8* fileDes)
{
   	int 	ix;
   	UINT32 readLen;
   	UINT32 writeLen;
   	UINT8* pReadData;
   	UINT8* pReadTmp;
   	UINT8* pWriteData;
   	UINT8* pWriteTmp;
   	STATUS retVal;

   	retVal = bspSizeOfFile (fileSrc, &readLen);
   	if (retVal != BSP_OK)
   	{
		return retVal;
   	}

	/* read source file */
   	pReadData = (UINT8*) malloc (readLen);
	if (pReadData == NULL)
	{
		return (BSP_ERR_MEM_MALLOC);
	}

	retVal = bspReadFile (fileSrc, readLen, pReadData, &readLen);
	if (retVal != BSP_OK)
	{
		free (pReadData);
		return retVal;
	}

	/* write destination file */
	pWriteTmp = pReadData;
	retVal = bspCreateFile (fileDes, pWriteTmp, readLen);
	if (retVal != BSP_OK)
	{
		free (pReadData);
		return retVal;
	}
	
	/* read destination file */
   	pWriteData = (UINT8*) malloc (readLen + 4);
	if (pReadData == NULL)
	{
		free (pReadData);
		return (BSP_ERR_MEM_MALLOC);
	}

	retVal = bspReadFile (fileDes, (readLen + 4), pWriteData, &writeLen);
	if (retVal != BSP_OK)
	{
		free (pReadData);
		free (pWriteData);
		return retVal;
	}
	
	/* compare data */
	pReadTmp = pReadData;
	pWriteTmp = pWriteData;
	for (ix = 0; ix < readLen; ix++)
	{
		if (*pReadTmp++ != *pWriteTmp++)
		{
			break;
		}

	}

	free (pReadData);
	free (pWriteData);
	
	if (ix == readLen)
	{
		printf ("copy %s to %s successfully.\n", fileSrc, fileDes);
		return (BSP_OK);
	}
	else
	{
		printf ("copy %s to %s fail.\n", fileSrc, fileDes);
		return (BSP_ERR_WRITE_FILE);
	}
    	
}


/******************************************************************
input:	none

output:	none

return:	BSP_OK

description:

do nothing.

******************************************************************/
STATUS bspIncludeFileDriver (void)
{
	return (BSP_OK);
}


#ifdef FILE_DRV_DEBUG



#endif
